package com.ff.neuropp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ff.neuropp.domain.CareerType;
import com.ff.neuropp.service.CareerTypeService;
import com.ff.neuropp.util.ResultTool;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/api/careertype")
@Api(value = "职业生涯类型管理controller", tags = {"职业生涯类型管理接口"})
public class PcCareerTypeController {

    @Autowired
    private CareerTypeService careerTypeService;

    @PostMapping("/create")
    @ApiOperation(value = "添加职业生涯类型信息",notes = "添加职业生涯类型信息")
    public Map<String,Object> add(@RequestBody CareerType careerType){
        CareerType type = careerTypeService.saveCareerType(careerType);
        return ResultTool.genSuccessResult(type);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改职业生涯类型信息", notes = "修改职业生涯类型信息")
    public Map<String,Object> updateCourseCategory(@RequestBody CareerType careerType){
        CareerType cc = careerTypeService.findById(careerType.getId());
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        CareerType updateCc = careerTypeService.saveCareerType(careerType);
        return ResultTool.genSuccessResult("Success",updateCc);
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询信息(PC)", notes = "根据id查询信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        CareerType ct = careerTypeService.findById(id);
        if (null == ct) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        return ResultTool.genSuccessResult("success",ct);
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "职业生涯类型分页", notes = "职业生涯类型分页")
    public Map<String, Object> findListPage(@Valid CareerType careerType,
                                            HttpServletRequest request) {
        Page<CareerType> list = careerTypeService.findPage(careerType);
        Map<String, Object> result4Page = new HashMap<>();
        result4Page.put("total",list.getTotalPages());
        result4Page.put("pageNo",list.getNumber()+1);
        result4Page.put("list",list.getContent());
        if(null !=list && list.getTotalElements()>0){
            return ResultTool.genSuccessResult("Success",result4Page);
        }else{
            return ResultTool.genSuccessResult("Error,no data！",result4Page);
        }

    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有信息", notes = "获取所有信息")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String name,
                                        HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != name && name.length() > 0) {
            param.put("LIKE_careerTypeName", name);
        }
        List<CareerType> list = careerTypeService.findList(param);
        if (null != list && list.size() > 0) {
            Map<String, Object> result = new HashMap<>();
            result.put("total",list.size());
            result.put("list",list);
            return ResultTool.genSuccessResult("success",result);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
    }


	@PostMapping("/status")
	@ApiOperation(value = "修改职业生涯类型状态", notes = "修改职业生涯类型状态")
    public  Map<String,Object> update(@ApiParam(required = true,value = "类型id") @RequestParam Integer id, @ApiParam(required = true,value = "状态") @RequestParam Integer status){
         CareerType cc = careerTypeService.findById(id);
         if(null == cc){ // 没查到该条数据
             return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
         }
         cc.setStatus(status);
         CareerType updateCc = careerTypeService.saveCareerType(cc);
         return ResultTool.genSuccessResult("Success",updateCc);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除职业生涯类型信息", notes = "删除职业生涯类型信息")
    public  Map<String,Object> deleteCourseCategory(@ApiParam(required = true,value = "职业生涯类型id") @RequestParam Integer id){
        CareerType cc = careerTypeService.findById(id);
        if (null == cc) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        careerTypeService.deleteById(id);
        return ResultTool.genSuccessResult("Success");

    }


}
